
$(function() {
	$("input[id^='mold_s']:visible").autocomplete(Test.mold);

	$("input[id^='material_s']:visible").autocomplete(Test.material);
		
	Tip.emp('申请人');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("申请人：");
		} else if (a == 'oper') {
			label.html("录入员：");
		} 
	});
});

var Bom = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.date = form.date.value;
				body.mold = form.mold.value;
				body.material = form.material.value;
				body.type = form.type.value;

				body.name = form.name.value;
				body.standard = _standard(form);
				body.price2 = form.price2.value;
				body.unit2 = form.unit2.value;
				body.density = form.density.value;

				body.unit = form.unit.value;
				body.amount = form.amount.value;
				body.used = form.used.value;
							
				body.content = form.content.value;
				body.emp = form.emp.value;

				$(this).dialog("close");

				$.post('material.php?do=Bom.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_cal_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.audit&item=75', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"计算": function() {
						if (!confirm("确认是否开始计算？")) {
							return false;
						}
						
						var form = $("#form_add")[0];

						var body = { };
						body.emp = form.emp.value;
						body.date1 = form.date1.value;
						body.date2 = form.date2.value;

						body.content = form.content.value;

						$(this).dialog("close");

						$.post('material.php?do=Bom.calPost', body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	clean_cache: function () {
		if (!confirm("确认是否清除缓存？")) {
			return false;
		}

		$.get('material.php?do=Material.ajax_category_subclass', function(data) {
			alert("操作成功！");
		});
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		$.cookie('status_', status);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=Bom.edit&id=' + id;
		var c2 = 'material.php?do=Bom.view&id=' + id;
		var c3 = 'material.php?do=Bom.copy&id=' + id;
		
		var c5 = 'material.php?do=Bom.status&id=' + id;

		var a1 = ['编辑信息', 'url', 'Bom.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];

		var a3 = ['复制信息', 'url', 'Bom.dialog_copy', '650x400'];

		// 物料申请
		if (status == 1) {
			var d1 = 'material.php?do=Bom.verify&id=' + id;
			var b1 = ['审核物料', 'url', 'Bom.dialog_verify', '650x420'];
			
			var s = d1 + ", " + c1 + ", " + c2 + ", " + c3;

			return this.popup_(e, [b1, a1, a2, a3], s);
		}
		
		// 等待采购
		if (status == 2) {
			var b1 = ['安排采购', 'url', 'Bom.dialog_record', '650x420'];
			var d1 = 'material.php?do=Bom.record&id=' + id;
			
			var b2 = ['直接采购', 'url', 'Bom.dialog_purchase', '680x420'];
			var d2 = 'material.php?do=Bom.purchase&id=' + id;
					
			var b3 = ['询价信息', 'url', 'Bom.dialog_enquiry', '650x520'];
			var d3 = 'material.php?do=Bom.enquiry&id=' + id;
			
			var s = d3 + ", " + d1 + ", " + d2 + ", " + c1 + ", " + c2 + ", " + c3;

			return this.popup_(e, [b3, b1, b2, a1, a2, a3], s);
		}
		
		// 正在采购
		if (status == 3) {
			var b1 = ['采购情况', 'url', 'Bom.dialog_record_in', '800x420'];
			var d1 = 'material.php?do=RecordIn.bom&bom_s=' + id;
			
			var s = d1 + ", " + c2 + ", " + c3;

			return this.popup_(e, [b1, a2, a3], s);
		}
		
		// 已经入库 使用库存
		if (status == 4 || status == 8) {
			var b1 = ['领料出库', 'url', 'Bom.dialog_out', '800x420'];
			var d1 = 'material.php?do=Out.add&bom=' + id;
			
			var b2 = ['出库信息', 'url', 'Bom.dialog_outlog', '800x420'];
			var d2 = 'material.php?do=Out.bom&bom_s=' + id;
			
			var s = d1 + ", " + d2 + ", " + c2 + ", " + c3;

			return this.popup_(e, [b1, b2, a2, a3], s);
		}

		var s = c2 + ", " + c3;

		return this.popup_(e, [a2, a3], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'material.php?do=Bom.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Bom.dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'material.php?do=Bom.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.ajax_emp_oper', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						
						body.date = form.date.value;
						body.mold = form.mold.value;
						body.material = form.material.value;
						body.type = form.type.value;
						
						body.name = form.name.value;
						body.standard = _standard(form);
						
						body.unit = form.unit.value;
						body.price = form.price.value;
						body.amount = form.amount.value;
						body.used = form.used.value;

						body.content = form.content.value;
						body.emp = form.emp.value;

						$(this).dialog("close");

						var url = 'material.php?do=Bom.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_copy: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.date = form.date.value;
				body.mold = form.mold.value;
				body.material = form.material.value;
				body.type = form.type.value;
				
				body.name = form.name.value;
				body.standard = _standard(form);
				body.price2 = form.price2.value;
				body.unit2 = form.unit2.value;
				body.density = form.density.value;
				
				body.unit = form.unit.value;
				body.price = form.price.value;
				body.amount = form.amount.value;

				body.used = form.used.value;

				body.content = form.content.value;
				body.emp = form.emp.value;

				$(this).dialog("close");

				var url = 'material.php?do=Bom.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_record: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.audit&item=62', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						body.id = form.id.value;
						body.purchase = form.purchase.value;

						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'material.php?do=Bom.recordPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_enquiry: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.audit&item=62', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"计算": function() {
						var form = $("#form_add")[0];

						var body = {};
						body.id = form.id.value;
											
						var volume = form.vvv.value;
						var density = form.density.value;
						var price = form.price.value;
						
						if (volume == null || volume == '') {
							alert("体积不能为空，请输入正确的数字！");
							return false;
						}
						
						if (density == null || density == '') {
							alert("密度不能为空，请输入正确的数字！");
							return false;
						}
						
						if (price == null || price == '') {
							alert("单价不能为空，请输入正确的数字！");
							return false;
						}
						
						var r = eval(volume) * eval(density) * eval(price) * Math.pow(10, -3);
						
						var total = fmoney(r, 3);
						
						$("#total").val(total);
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_purchase: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.audit&item=62', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						body.id = form.id.value;
						
						body.emp = form.emp.value;					
						
						body.name = form.name.value;
						body.date = form.date.value;
						
						body.customer = form.customer.value;
						body.customer_x = Common.customer_x(form);
						
						body.address = form.address.value;
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'material.php?do=Purchase.purchasePost';

						$.post(url, body, function(data) {
							// alert("操作成功！");
							Bom.list_();
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.audit&item=61', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不同意": function() {
						if (!confirm("确认是否审核不通过？")) {
							return false;
						}
						
						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 9;
						
						var url = 'material.php?do=Bom.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"安排采购": function() {
						if (!confirm("确认是否安排采购？")) {
							return false;
						}

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						
						body.status = 2;
						
						var url = 'material.php?do=Bom.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"使用库存": function() {
						if (!confirm("确认是否使用库存？")) {
							return false;
						}

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						
						body.status = 8;
						
						var url = 'material.php?do=Bom.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	out_validate: function () {
		var validator = $("#form_add").validate({
			rules: {
				applicant: {
					required: true
				},
				amount: "required"
			},
			messages: {
				applicant: {
					required: "请输入领料人"
				},
				amount: {
					required: "请输入出库的数量"
				}
			},
			errorPlacement: function(label, element) {
				if (/^place/.test(element[0].name)) {
					label.insertAfter("#place_t");
				} else {
					label.insertAfter(element);
				}
			},
			success: function(label) {
				label.html("&nbsp;").addClass("checked");
			}
		});

		if (!validator.form()) {
			return false;
		}

		return true;
	},

	dialog_out: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Amount.audit&item=57', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"出库": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						if (!Bom.out_validate()) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						body.id = form.id.value;
						body.applicant = form.applicant.value;					
						body.amount = form.amount.value;
						body.mold = form.mold.value;					
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'material.php?do=Out.addPost';

						$.post(url, body, function(data) {
							alert("操作成功！");
							Bom.list_();
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_outlog: function (url, title, xy) {
		$('#dialog_view').dialog('option', 'buttons', {

			"关闭": function() {
				$(this).dialog("close");
			}
		});

		dialog_view(url, title, xy);
	},

	dialog_record_in: function (url, title, xy) {
		$('#dialog_view').dialog('option', 'buttons', {

			"关闭": function() {
				$(this).dialog("close");
			}
		});

		dialog_view(url, title, xy);
	},

	all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=Bom.delete' + p2;
		var c2 = 'material.php?do=Bom.batch_verify';
		var c3 = 'material.php?do=Bom.batch_enquiry';
		var c4 = 'material.php?do=Bom.batch_record';
		
		var c9 = 'material.php?do=Bom.xls' + p2;
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		var a2 = ['审核信息', 'url', 'Bom.dialog_batch_verify', '800x450', '审核信息'];
		var a3 = ['采购询价', 'url', 'Bom.dialog_batch_enquiry', '650x400', '采购询价'];
		var a4 = ['安排采购', 'url', 'Bom.dialog_batch_record', '650x420', '安排采购'];
		
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2 + ", " + c3 + ", " + c4 + ", " + c9;

		return popup(e, [a1, a2, a3, a4, a9], s);
	},

	dialog_batch_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.audit&item=61', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_1_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不同意": function() {
						if (!confirm("确认是否审核不通过？")) {
							return false;
						}
						
						var cc = Bom._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.content = form.content.value;
						body.status = 9;
						
						var url = 'material.php?do=Bom.batch_verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"安排采购": function() {
						if (!confirm("确认是否安排采购？")) {
							return false;
						}
						
						var cc = Bom._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.content = form.content.value;					
						body.status = 2;
						
						var url = 'material.php?do=Bom.batch_verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"使用库存": function() {
						if (!confirm("确认是否使用库存？")) {
							return false;
						}

						var cc = Bom._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.content = form.content.value;					
						body.status = 8;
						
						var url = 'material.php?do=Bom.batch_verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},

	dialog_batch_record: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.audit&item=62', function(json) {
			if (json.status == 1) {
				var c = new Array();
				$("input[name^='c_2_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var cc = Bom._cc();

						var form = $("#form_add")[0];
						
						var body = {};
						body.cc = cc;
						body.purchase = form.purchase.value;
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'material.php?do=Bom.batch_recordPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_add';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});
	},
	
	dialog_batch_enquiry: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Bom.audit&item=62', function(json) {
			if (json.status == 1) {
				var c = new Array();
				$("input[name^='c_2_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"发送": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];
						
						var body = {};
						body.from = form.from.value;
						body.to = form.to.value;
						body.subject = form.subject.value;
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'material.php?do=Bom.mailPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							alert("发送成功！");
						});
					}
				});

				var view = '#dialog_add';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});
	},

	_cc: function () {
		var c = new Array();
		$("input[name^='b_']").each(function() {
			if ($(this).attr("checked")) {
				c.push($(this).val());
			}
		});
		
		return c;
	},

	list_: function () {
		var result = Common.result();
		var url = 'material.php?do=Bom.findPost';

		var p2 = $.cookie('p2');
		if (p2 != null) {
			url += p2;
		}

		$.get(url, function(data) {
			$(result).html(data);
		});
	},

	expunge_: function (form) {
		$.getJSON('material.php?do=Material.audit&item=52', function(json) {
			if (json.status == 1) {
				expunge(form);
			} else {
				_error();
			}
		});
	}
};
